package com.cskaoyan.shopping.controller.hxj;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.CheckAllItemRequest;
import com.cskaoyan.shopping.dto.CheckAllItemResponse;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Tiger
 * @since 2022/06/17 16:23
 */
@RestController
public class AllCartsItemsController {

    @Autowired
    ICartService cartService;

    @PutMapping("/shopping/items")
    public ResponseData checkAllCartsItems(@RequestBody Map map){
        String id = (String) map.get("userId");
        Boolean check = (Boolean) map.get("checked");
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        long userId = Long.parseLong(id);
        String checked = String.valueOf(check);
        checkAllItemRequest.setChecked(checked);
        checkAllItemRequest.setUserId(userId);

        CheckAllItemResponse response = cartService.checkAllCartItem(checkAllItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
